from selenium import webdriver
import openpyxl
from selenium.webdriver.chrome.options import Options
import re
import time
import pandas


class Amazon():
    def __init__(self):
        self.chrome_options = Options()
        self.chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
        self.browser = webdriver.Chrome(options=self.chrome_options)
        self.wb = openpyxl.Workbook()
        self.ws = self.wb.create_sheet('sheet1')
        self.link = []
        self.name = []
        self.link2 = []
        self.name2 = []
        self.link3 = []
        self.name3 = []
        self.link4 = []
        self.name4 = []
        self.link5 = []
        self.name5 = []
        self.link6 = []
        self.name6 = []
        self.three_chico = []
        self.num3 = -1
        self.list01 = []

    def link_one(self):

        zong = self.browser.page_source

        re_zong = re.findall('<ul id="zg_browseRoot">(.*?)</ul>', zong, re.S)[0]

        re_li = re.findall('<li>(.*?)</li>', re_zong)
        name01 = []

        for i in re_li:
            name01.clear()
            # 第一层链接
            link = re.findall('href="(.*?)"', i)[0]
            print('*' * 10, '第一层链接', link)
            self.link.append(link)
            # 第一层目录名
            one_name = re.findall(">(.*?)</a>", i)[0]
            # 将目录名的数据类型转换为列表
            name01.append(one_name)
            # 插入第一层目录名称
            # self.ws.cell(row=self.num01, column=1).value = f'{one_name}'
            print('*' * 10, '第一层目录', one_name)
            self.name.append(one_name)
            self.link_two(name01)
        l = pandas.DataFrame(self.list01)
        l.to_excel('amazon.xlsx', index=True)

    def link_two(self, name01):

        for i22 in self.name:

            i22 = i22.replace('&amp;', '&').replace('&nbsp;', '')
            print('2')
            self.browser.find_element_by_link_text(f'{i22}').click()
            time.sleep(3)
            zong2 = self.browser.page_source

            re_zong2 = re.findall('<ul id="zg_browseRoot">(.*?)</ul>', zong2, re.S)[0]

            re_li2 = re.findall('<li>(.*?)</li>', re_zong2)

            for i2 in re_li2:
                # 第二层链接
                link2 = re.findall('href="(.*?)"', i2)[0]
                print('*' * 10, '第二层链接', link2)
                self.link2.append(link2)
                # 第二层目录名
                two_name = re.findall(">(.*?)</a>", i2)[0]
                print('*' * 10, '第二层目录', two_name)
                # 插入第二层目录名称
                name01.append(two_name)
                self.name2.append(two_name)
                self.link_three(link2, name01)
                self.name.clear()
            time.sleep(5)
            self.browser.back()

    def link_three(self, link2, name01):
        # print('3' * 10)
        for i33 in self.name2:

            i33 = i33.replace('&amp;', '&').replace('&nbsp;', '')

            self.browser.get(link2)

            time.sleep(3)
            zong3 = self.browser.page_source

            re_zong3 = re.findall('<ul id="zg_browseRoot">(.*?)</ul>', zong3, re.S)[0]

            re_li3 = re.findall('<li>(.*?)</li>', re_zong3)

            try:
                three_chico = self.browser.find_element_by_xpath(
                    "//div[@id='a-page']/div[@id='zg']/div[@class='a-fixed-left-flipped-grid']/div[@class='a-fixed-left-grid-inner']/div[@id='zg-left-col']/ul[@id='zg_browseRoot']/ul/ul/ul")
            except Exception as g:
                three_chico = self.browser.find_element_by_xpath(
                    "//div[@id='a-page']/div[@id='zg']/div[@class='a-fixed-left-flipped-grid']/div[@class='a-fixed-left-grid-inner']/div[@id='zg-left-col']/ul[@id='zg_browseRoot']/ul/ul")

            self.name2.clear()
            if i33 not in three_chico.text:

                # try:

                for i3 in re_li3:
                    # 第三层链接
                    link3 = re.findall('href="(.*?)"', i3)[0]
                    print('*' * 10, '第三层链接', link3)
                    self.link3.append(link3)
                    # 第三层目录名
                    three_name = re.findall(">(.*?)</a>", i3)[0]
                    print('*' * 10, '第三层目录', three_name)
                    # 插入第三层目录名
                    name01.append(three_name)
                    self.name3.append(three_name)
                    self.link_four(link3, name01)
                    self.name2.clear()

                    # time.sleep(5)
                self.browser.back()
            # except Exception as g:
            #     print('第三层无下一级')
            #     self.name2.clear()
            #     self.browser.back()
            # return self.link3, self.name3
            else:
                print('第三层无下一级')
                self.list01.append(name01)
                name01 = name01[:-1]
                time.sleep(3)
                self.browser.back()

    def link_four(self, link3, name01):
        for i44 in self.name3:
            i44 = i44.replace('&amp;', '&').replace('&nbsp;', ' ')

            self.browser.get(link3)
            print('4')
            time.sleep(3)
            zong4 = self.browser.page_source

            re_zong4 = re.findall('<ul id="zg_browseRoot">(.*?)</ul>', zong4, re.S)[0]

            re_li4 = re.findall('<li>(.*?)</li>', re_zong4)

            try:
                three_chico4 = self.browser.find_element_by_xpath(
                    "//div[@id='a-page']/div[@id='zg']/div[@class='a-fixed-left-flipped-grid']/div[@class='a-fixed-left-grid-inner']/div[@id='zg-left-col']/ul[@id='zg_browseRoot']/ul/ul/ul/ul")
            except Exception as g:
                three_chico4 = self.browser.find_element_by_xpath(
                    "//div[@id='a-page']/div[@id='zg']/div[@class='a-fixed-left-flipped-grid']/div[@class='a-fixed-left-grid-inner']/div[@id='zg-left-col']/ul[@id='zg_browseRoot']/ul/ul/ul")

            self.name3.clear()

            if i44 not in three_chico4.text:
                # try:
                for i4 in re_li4:
                    # 第四层链接
                    link4 = re.findall('href="(.*?)"', i4)[0]
                    print('*' * 10, '第四层链接', link4)
                    self.link4.append(link4)
                    # 第四层目录名
                    four_name = re.findall(">(.*?)</a>", i4)[0]
                    print('*' * 10, '第四层目录', four_name)
                    self.name4.append(four_name)
                    self.name3.clear()
                    name01.append(four_name)
                    self.link_five(link4, name01)
                self.browser.back()
            # except Exception as g:
            #     print('第四层无下一级')
            #     self.name3.clear()
            #     self.browser.back()
            else:
                print('第四层无下一级')
                self.list01.append(name01)
                name01 = name01[:-1]
                time.sleep(3)
                self.browser.back()

    def link_five(self, link4, name01):
        for i55 in self.name4:
            i55 = i55.replace('&amp;', '&').replace('&nbsp;', '')

            self.browser.get(link4)
            print('5')
            time.sleep(3)
            zong5 = self.browser.page_source

            re_zong5 = re.findall('<ul id="zg_browseRoot">(.*?)</ul>', zong5, re.S)[0]

            re_li5 = re.findall('<li>(.*?)</li>', re_zong5)

            try:
                three_chico5 = self.browser.find_element_by_xpath(
                    "//div[@id='a-page']/div[@id='zg']/div[@class='a-fixed-left-flipped-grid']/div[@class='a-fixed-left-grid-inner']/div[@id='zg-left-col']/ul[@id='zg_browseRoot']/ul/ul/ul/ul/ul")
            except Exception as g:
                three_chico5 = self.browser.find_element_by_xpath(
                    "//div[@id='a-page']/div[@id='zg']/div[@class='a-fixed-left-flipped-grid']/div[@class='a-fixed-left-grid-inner']/div[@id='zg-left-col']/ul[@id='zg_browseRoot']/ul/ul/ul/ul")

            self.name4.clear()

            if i55 not in three_chico5.text:
                # try:
                for i5 in re_li5:
                    # 第五层链接
                    link5 = re.findall('href="(.*?)"', i5)[0]
                    print('*' * 10, '第五层链接', link5)
                    self.link5.append(link5)
                    # 第五层目录名
                    five_name = re.findall(">(.*?)</a>", i5)[0]
                    print('*' * 10, '第五层目录', five_name)
                    self.name5.append(five_name)
                    self.name4.clear()
                    name01.append(five_name)
                    self.link_six(link5, name01)
                self.browser.back()
            # except Exception as g:
            #     print('第四层无下一级')
            #     self.name4.clear()
            #     self.browser.back()
            else:
                print('第五层无下一级')
                self.list01.append(name01)
                name01 = name01[:-1]
                time.sleep(3)
                self.browser.back()

    def link_six(self, link5, name01):
        for i66 in self.name5:
            i66 = i66.replace('&amp;', '&').replace('&nbsp;', '')

            self.browser.get(link5)
            print('6')
            time.sleep(3)
            zong6 = self.browser.page_source

            re_zong6 = re.findall('<ul id="zg_browseRoot">(.*?)</ul>', zong6, re.S)[0]

            re_li6 = re.findall('<li>(.*?)</li>', re_zong6)

            try:
                three_chico6 = self.browser.find_element_by_xpath(
                    "//div[@id='a-page']/div[@id='zg']/div[@class='a-fixed-left-flipped-grid']/div[@class='a-fixed-left-grid-inner']/div[@id='zg-left-col']/ul[@id='zg_browseRoot']/ul/ul/ul/ul/ul/ul")
            except Exception as g:
                three_chico6 = self.browser.find_element_by_xpath(
                    "//div[@id='a-page']/div[@id='zg']/div[@class='a-fixed-left-flipped-grid']/div[@class='a-fixed-left-grid-inner']/div[@id='zg-left-col']/ul[@id='zg_browseRoot']/ul/ul/ul/ul/ul")

            self.name5.clear()

            if i66 not in three_chico6.text:
                # try:
                for i6 in re_li6:
                    # 第六层链接
                    link6 = re.findall('href="(.*?)"', i6)[0]
                    print('*' * 10, '第六层链接', link6)
                    self.link6.append(link6)
                    # 第六层目录名
                    six_name = re.findall(">(.*?)</a>", i6)[0]
                    print('*' * 10, '第六层目录', six_name)
                    self.name6.append(six_name)
                    self.name5.clear()
                    name01.append(six_name)
                self.browser.back()
            # except Exception as g:
            #     print('第四层无下一级')
            #     self.name4.clear()
            #     self.browser.back()
            else:
                print('第六层无下一级')
                self.list01.append(name01)
                name01 = name01[:-1]
                time.sleep(3)
                self.browser.back()


if __name__ == '__main__':
    nico = Amazon()
    nico.link_one()
    # nico.link_two()
    # nico.link_three()
    # nico.link_four()
